const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const userRoute = require('./routes/users')
const routeRoute = require('./routes/routes')
const indexRoute = require('./routes/index')

const app = express()
const path = require('path')

require('./utils/checkAndCreateAdmin')

// app.use(express.static(path.join(__dirname, "public")));
app.use('/uploads', express.static(path.join(__dirname, '../uploads')))

// 引入body-parser模块，用于解析请求体中的JSON数据
app.use(bodyParser.json())
// 引入body-parser模块，用于解析请求体中的URL编码数据，其实就是普通表单格式
app.use(bodyParser.urlencoded({ extended: false }))
// 引入cors模块，用于处理跨域请求
app.use(cors())

// 注册路由
app.use('/', indexRoute)
app.use('/api', userRoute)
app.use('/api', routeRoute)

// 启动服务器
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
  console.log(`http://localhost:${PORT}`)
})
